/**
 * 
 */
package reuseplugin.handlers;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.handlers.HandlerUtil;

import reuseplugin.editors.EintragsEditor;
import reuseplugin.editors.EintragsEditorInput;
import reuseplugin.views.eintragsListe.EintragsView;

import common.itemhandling.Eintrag;

/**
 * @author Julius Ollesch
 * 
 */
public class CallEintragsEditor extends AbstractHandler implements IHandler {
	public static final String ID = "reuseplugin.commands.CallEintragsEditor";

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
		IWorkbenchPage page = window.getActivePage();
		EintragsView view = (EintragsView) page.findView(EintragsView.ID);
		// Get the selection
		ISelection selection = view.getSite().getSelectionProvider()
				.getSelection();
		if (selection != null && selection instanceof IStructuredSelection) {
			Object obj = ((IStructuredSelection) selection).getFirstElement();
			// If we had a selection lets open the editor
			if (obj != null) {
				Eintrag eintrag = (Eintrag) obj;
				EintragsEditorInput input = new EintragsEditorInput(eintrag);
				try {
					page.openEditor(input, EintragsEditor.ID);

				} catch (PartInitException e) {
					System.out.println(e.getStackTrace());
				}
			}
		}
		return null;
	}
}
